package com.ruoyi.systemapp.utils;

import com.google.protobuf.ByteString;
import com.mysql.cj.util.TimeUtil;
import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.constant.MsgConstants;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.ProtoCode;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.JwtUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.system.api.model.LoginUser;
import io.jsonwebtoken.Claims;
import io.micrometer.core.instrument.util.TimeUtils;
import proto.client.ProtoCmd;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Author:cgChen
 * Date:2022/05/17 16:23
 * Description:响应数据公共类
 */
public class ResponseResult {

    /**
     * 响应信息（成功、失败提示）
     * @param status 响应状态
     * @param msg 消息
     * @return
     */
    public static byte[] responseStatus(int status,String msg,int code){
        ProtoCmd.ResponseCmd.Builder responseCmd=ProtoCmd.ResponseCmd.newBuilder();
        responseCmd.setPact(Constants.PACT);
        responseCmd.setCode(code);
        responseCmd.setStatus(status);
        responseCmd.setMsg(msg);
        return responseCmd.build().toByteArray();
    }

    /**
     * 响应结果
     * @return
     */
    public static byte[] responseMap(int status, String msg, int code, ByteString result){
        ProtoCmd.ResponseCmd.Builder responseCmd=ProtoCmd.ResponseCmd.newBuilder();
        responseCmd.setPact(Constants.PACT);
        responseCmd.setCode(code);
        responseCmd.setStatus(status);
        responseCmd.setMsg(msg);
        responseCmd.setResult(result);
        return responseCmd.build().toByteArray();
    }

    /**
     * 校验jwtToken是否失效
     * @param redisService
     * @param jwtToken
     * @return
     */
    public static LoginUser isLogin(RedisService redisService,String jwtToken){
        Claims claims = JwtUtils.parseToken(jwtToken);
        String token= claims.get(SecurityConstants.USER_KEY).toString();
        String redisKey=RedisService.getTokenKey(token);
        if(!redisService.hasKey(redisKey)){
            return null;
        }
        return redisService.getCacheObject(redisKey);
    }



}
